package fxbattle.fxgui;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.transform.Transform;
import java.lang.Math;
import javafx.scene.input.MouseEvent;
import fxbattle.client.model.Direction;
import fxbattle.fxgui.EqulateralTriangle;

/**
 * @author spina
 */
def cos60 = Math.cos(Math.toRadians(60));
var pad = 2;
def sin60 = Math.sin(Math.toRadians(60));
def stroke = 2;

public class Hexagon extends CustomNode {
    public var color: Color = Color.WHITESMOKE on replace {
        for(tri in triangles){
            tri.color = color;
        }
    };;
	public var size: Number;
    var triangles : EqulateralTriangle[];
    public function mousePressedOnDirection(dir: Direction){}
    public function mouseEnteredTriangle(dir: Direction): Void{}
    public function mouseExitedTriangle(dir: Direction): Void{}

    public function makeTriangle(dir: Direction): EqulateralTriangle{
        var tri: EqulateralTriangle = EqulateralTriangle {
                visible: visible
                size : size
                color : color;
                onMouseEntered: function( e: MouseEvent ): Void {
                    //println("Mouse Entered: {e.x}, {e.y}");
                    mouseEnteredTriangle(dir);
                }

                onMouseExited: function( e: MouseEvent ): Void {
                    mouseExitedTriangle(dir);
                }
                onMousePressed: function( e: MouseEvent ): Void {
                    mousePressedOnDirection(dir);
                }
        };

    }

    public override function create(): Node {
        for(dir in Direction.values()){
            var tri = makeTriangle(dir);
            var trans = Transform.rotate(dir.angle() - 60, 0, sin60 * size);
            insert trans into tri.transforms;
        	insert tri into triangles;
    	}
	    return Group {
            transforms : [Transform.translate(0, - size + pad + 5)]
            content: triangles
        };
    }  
}
